package com.fuyao.cloud.admin.flow.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.csp.sentinel.util.AssertUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuyao.cloud.admin.flow.leave.Leave;
import com.fuyao.cloud.admin.flow.leave.LeaveTypeEnum;
import com.fuyao.cloud.admin.flow.service.org.ILeaveService;
import com.fuyao.cloud.common.core.constant.CommonConstants;
import com.fuyao.cloud.common.core.controller.FlowableController;
import com.fuyao.cloud.common.core.flowable.enums.ProcessStatusEnum;
import com.fuyao.cloud.common.core.util.R;
import com.fuyao.cloud.common.log.annotation.SysLog;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/hr/leave")
public class LeaveController implements FlowableController {

    @Autowired
    private ILeaveService leaveService;

    /**
     * 分页查询
     *
     * @param page       分页对象
     * @param tblHrLeave
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public R getTblHrLeavePage(Page page, Leave tblHrLeave) {
        return R.ok(leaveService.page(page, Wrappers.query(tblHrLeave).orderByDesc("create_time")));
    }


    /**
     * 通过id查询
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") String id) {
        return R.ok(leaveService.getById(id));
    }

    /**
     * 新增
     *
     * @param tblHrLeave
     * @return R
     */
    @ApiOperation(value = "新增", notes = "新增")
    @SysLog("新增")
    @PostMapping
    public R save(@RequestBody Leave tblHrLeave) {
        return R.ok(leaveService.save(tblHrLeave));
    }

    /**
     * 修改
     *
     * @param tblHrLeave
     * @return R
     */
    @ApiOperation(value = "修改", notes = "修改")
    @SysLog("修改")
    @PutMapping
    public R updateById(@RequestBody Leave tblHrLeave) {
        return R.ok(leaveService.updateById(tblHrLeave));
    }

    /**
     * 通过id删除
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @SysLog("通过id删除")
    @DeleteMapping("/{id}")
    public R removeById(@PathVariable String id) {
        return R.ok(leaveService.removeById(id));
    }

    @GetMapping(value = "/getLeaveById/{id}", produces = "application/json")
    public R getLeaveById(@PathVariable String id) {
        Leave leave = leaveService.getLeaveById(id);
        return R.ok(leave);
    }

    @PostMapping(value = "/addLeave", produces = "application/json")
    public R<Leave> addLeave(@RequestBody Leave leave) {
        String businessKey = IdWorker.get32UUID();
        String enumMsgByType = LeaveTypeEnum.getEnumMsgByType(Integer.valueOf(leave.getType()));
        String time = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");
        leave.setTitle("请假流程" + "-" + enumMsgByType + " " + time);
        leave.setStatus(0);
        leave.setId(businessKey);
        leave.setCreateTime(new Date());
        this.leaveService.saveLeave(leave);
        return R.ok(leave);
    }

    @ApiOperation(value = "起草环节结束回调方法", notes = "起草环节结束回调方法")
    @PostMapping(value = "/draftEndCallback", produces = "application/json")
    @Override
    public R draftEndCallback(HttpServletRequest request, @RequestBody Map<String, Object> params) {
        if (params.containsKey("businessKey")) {
            Leave leave = this.leaveService.getById(params.get("businessKey").toString().split(":")[1]);
            leave.setStatus(1);
            this.leaveService.updateById(leave);
            return R.ok(CommonConstants.SUCCESS,"修改状态成功");
        }
        return R.ok(CommonConstants.SUCCESS,"起草环节结束回调方法");
    }

    @ApiOperation(value = "回退至起草环节回调方法", notes = "回退至起草环节回调方法")
    @PostMapping(value = "/returnCallback", produces = "application/json")
    @Override
    public R returnCallback(HttpServletRequest request, @RequestBody Map<String, Object> params) {
        if (params.containsKey("businessKey")) {
            Leave leave = this.leaveService.getById(params.get("businessKey").toString().split(":")[1]);
            leave.setStatus(1);
            this.leaveService.updateById(leave);
            return R.ok(CommonConstants.SUCCESS,"修改状态成功");
        }
        return R.ok(CommonConstants.SUCCESS,"回退至起草环节回调方法");
    }

    @ApiOperation(value = "流程结束回调方法", notes = "流程结束回调方法")
    @PostMapping(value = "/callback", produces = "application/json")
    @Override
    public R finishEndCallback(HttpServletRequest request, @RequestBody Map<String, Object> params) {
        if (params.containsKey("businessKey")) {
            Leave leave = this.leaveService.getById(params.get("businessKey").toString().split(":")[1]);
            int status = params.containsKey(ProcessStatusEnum.ZZ.toString()) ? 99 : 2;
            leave.setStatus(status);
            this.leaveService.updateById(leave);
        }
        return R.failed("未找到对应的业务数据");
    }

}
